﻿/*************************************************************************
 * Created by OhZee Creative, LLC
 * 2013
 * This code is licensed under Microsoft Public License (Ms-PL)
 * http://opensource.org/licenses/ms-pl
 * http://ozframework.codeplex.com/
 *************************************************************************/

namespace OhZeeCreative.Helpers
{
    using System;

    public static class FriendlyHelper
    {
        private static long Kilo = 1024;
        private static long OneKB = 1 * Kilo;
        private static long OneMB = OneKB * Kilo;
        private static long OneGB = OneMB * Kilo;

        public static string ByteSizeToFriendlyString(long bytes, string preFix = "", string postFix = "")
        {
            string text = string.Empty;

            if (bytes < 0)
            {
                text = "Invalid size";
            }
            else if (bytes < OneKB)
            {
                text = string.Format("{0} bytes", bytes);
            }
            else if (bytes < OneMB)
            {
                text = string.Format("{0} KB", bytes / OneKB);
            }
            else if (bytes < OneGB)
            {
                text = string.Format("{0} MB", bytes / OneMB);
            }
            else
            {
                text = string.Format("{0} GB", bytes / OneGB);
            }

            if (preFix == null)
            {
                preFix = string.Empty;
            }
            if (postFix == null)
            {
                postFix = string.Empty;
            }

            return preFix + text + postFix;
        }

        public static string TimeSpanToFriendlyString(TimeSpan duration)
        {
            string text;

            if (duration < TimeSpan.FromMinutes(1))
            {
                text = string.Format("{0} seconds", duration.TotalSeconds);
            }
            else if (duration < TimeSpan.FromHours(1))
            {
                text = string.Format("{0:0.0} minutes", duration.TotalMinutes);
            }
            else
            {
                text = string.Format("{0:0.0} hours", duration.TotalHours);
            }

            return text;
        }
    }
}
